package com.linwen.excise.base;

import java.util.ArrayList;
import java.util.List;

public class 螺旋遍历二维数组 {
    public static void main(String[] args) {
        int [][] arr = new int[][]{
                {1,2,3,4,5,6},
                {18,19,20,21,22,7},
                {17,28,29,30,23,8},
                {16,27,26,25,24,9},
                {15,14,13,12,11,10}};
        int [][] arr1 = new int [][]{
                {1,2,3},
                {8,9,4},
                {7,6,5}
        };
        System.out.println();
        List<Integer> result = 螺旋遍历二维数组.function(arr);
        for (int i = 0; i < result.size(); i++) {
            System.out.print(result.get(i)+"  ");
        }
    }
    public static List<Integer> function(int arr[][]){
        List<Integer> result = new ArrayList<Integer>();
        int m = arr.length;
        System.out.println("y方向长度"+m);
        int n = arr[0].length;
        System.out.println("x方向长度"+n);
        //外层循环的次数
        for (int i=0;i<(Math.min(m,n)+1)/2;i++){
            //上
            for(int up=i;up<n-i;up++){
                System.out.print(arr[i][up]+" ");
            }
            //右
            for(int right=i+1;right<m-i-1;right++){
                System.out.print(arr[right][m-i]+" ");
            }
            //下
            if((i+1)<((Math.min(m,n)+1)/2)){
                for(int down=n-1-i;down>i-1;down--){
                    System.out.print(arr[m-i-1][down]+" ");
                }
            }
            //左
            for(int left=i+1;left<m-i-1;left++){
                System.out.print(arr[m-left-1][i]+" ");
            }
        }
        return result;
    }

}
